<!---
Block archiving for organization that has users, workshops, orders, funds
persmissions
--->

<cfparam name="Organization_ID" type="integer" default="0" />
<cfparam name="returnView" type="string" default="organizations" />
<cfparam name="passback" type="string" default="" />

<cfscript>
	organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
	organization = organizationGateway.get(Organization_ID = Organization_ID);
</cfscript>

<cfif IsNumeric(organization.getOrganization_ID())>
	
	<cfscript>
		base = new assets.calgrants.com.Base();
		passbackQueryString = base.passbackToQueryString(passback);
		//qOrganization = organizationGateway.query(Organization_ID = organization.getOrganization_ID());
		countyGateway = new assets.calgrants.com.gateway.CountyGateway();
		counties = countyGateway.query(Region_ID = IIf(IsUserInRole("Organizer") OR IsUserInRole("Site Contact"), request.authUser.regionID, ""));
	</cfscript>
	
	<script type="text/javascript" language="javascript">
	<!--
	
		<cfoutput>
			var countyID = #organization.getCounty_ID()#;
			var counties =  new kendo.data.DataSource();
			counties.data(#SerializeJSON(base.QueryToStruct(counties), false)#);
		</cfoutput>
		
		validate = function() {
			var errors = "";
			var valid = true;
			//validation logic
			if (!valid) {
				alert(errors);
			}
			return valid;				
		}
		
		$(function() {			
			countyDropdown = $("#County_ID").kendoDropDownList({
				dataTextField: "county",
				dataValueField: "county_id",
				dataSource: counties,
				template: 	'<strong>${county}</strong><br>' +
							'${region_id} - ${region} Region'
			}).data("kendoDropDownList");
			countyDropdown.value(countyID);
			$("#organizationModal").modal();
			resizeModal("organizationModal");	
		})
	//-->
	</script>
	
	<cfoutput>
	
		<cfform id="organizationForm" name="organizationForm" action="#script_name#" method="post" onsubmit="return validate();">
			<cfinput type="hidden" name="action" value="saveOrganization" />
			<cfinput type="hidden" name="view" value="#returnView#" />
			<cfinput type="hidden" name="Organization_ID" value="#organization.getOrganization_ID()#" />
			<cfif IsDefined("passback")>
				<cfloop index="keyValue" list="#passback#" delimiters=";">
					<cfinput type="hidden" name="#ListFirst(keyValue)#" value="#ListLast(keyValue)#" />
				</cfloop>
			</cfif>
			
			<div id="organizationModal" class="modal modal-fullscreen" data-backdrop="static">
				<div class="modal-header">
					<h3>Organization</h3>
				</div>
				<div class="modal-body">
					<div class="row-fluid">
						<div class="span4">
							<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
								<div class="clearfix">
									<label class="required">Status</label>
									<cfselect name="Approved" required="yes" message="Status is required" class="span9">
										<option value="0"<cfif organization.getApproved() eq 0> selected</cfif>>Not approved</option>
										<option value="1"<cfif organization.getApproved() eq 1> selected</cfif>>Approved</option>
									</cfselect>
								</div>
							<cfelse>
								<div class="text-control clearfix">
									<label>Status</label>
									<input type="hidden" name="Approved" value="#organization.getApproved()#" />
									<cfif organization.getOrganization_ID() eq 0>
										New
									<cfelseif organization.getApproved() eq 1>
										Approved
									<cfelse>
										Pending Approval
									</cfif>
								</div>
							</cfif>
							<div class="clearfix">
								<label class="required">Name</label>
								<cfinput type="text" name="Organization" value="#organization.getOrganization()#" required="yes" message="Name is required" maxlength="200" class="span9">
							</div>
							<div class="clearfix">
								<label class="required">Address</label>
								<cfinput type="text" name="Address" value="#organization.getAddress()#" required="yes" message="Address is required" maxlength="200" class="span9">
							</div>
							<div class="clearfix">
								<label class="required">City</label>
								<cfinput type="text" name="City" value="#organization.getCity()#" required="yes" message="City is required" maxlength="100" class="span9">
							</div>
							<div class="clearfix">
								<label class="required">Zip Code</label>
								<cfinput type="text" name="Zip_Code" value="#organization.getZip_Code()#" required="yes" message="Zip Code is required and must be a valid zip code" validate="zipcode" maxlength="5" class="span9">
							</div>
							<div class="kendo-control clearfix">
								<label class="required">County</label>
								<cfselect name="County_ID" required="yes" message="County is required" class="span9" style="margin: 0;"></cfselect>
							</div>
							<cfif organization.getOrganization_ID() gt 0 AND (IsUserInRole("Super User") or IsUserInRole("Regional Admin"))>
								<div class="text-control clearfix">
									<label>ID</label>
									#organization.getOrganization_ID()#
								</div>
								<div class="text-control clearfix">
									<label>Date Created</label>
									#DateFormat(organization.getDate_Created(), "mmmm d yyyy")# @ #TimeFormat(organization.getDate_Created(), "h:mmtt")#
								</div>
							</cfif>
						</div>
						<div class="span8">
							<cfif (IsUserInRole("Super User") OR IsUserInRole("Regional Admin")) AND organization.getOrganization_ID() gt 0>
								<!--- <cfset Organization_ID = organization.getOrganization_ID() /> --->
						
								<!--- <div class="accordion" id="organizationAccordian">
									<div class="accordion-group">
										<div class="accordion-heading">
											<a class="accordion-toggle" data-toggle="collapse" data-parent="##organizationAccordian" href="##panel1">Users</a>
										</div>
										<div id="panel1" class="accordion-body collapse">
											<div class="accordion-inner">
												<cfinclude template="#request.pathToUI#/lists/users.cfm">
											</div>
										</div>
									</div>
									<div class="accordion-group">
										<div class="accordion-heading">
											<a class="accordion-toggle" data-toggle="collapse" data-parent="##organizationAccordian" href="##panel2">Workshops</a>
										</div>
										<div id="panel2" class="accordion-body collapse">
											<div class="accordion-inner">
												<cfinclude template="#request.pathToUI#/lists/workshops.cfm">
											</div>
										</div>
									</div>
									<div class="accordion-group">
										<div class="accordion-heading">
											<a class="accordion-toggle" data-toggle="collapse" data-parent="##organizationAccordian" href="##panel3">Orders</a>
										</div>
										<div id="panel3" class="accordion-body collapse">
											<div class="accordion-inner">
												<cfinclude template="#request.pathToUI#/lists/orders.cfm">
											</div>
										</div>
									</div>
								</div> --->
							
							</cfif>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<a href="?view=#returnView#&#passbackQueryString#" class="btn btn-success">Cancel</a>
					<cfif IsUserInRole("Super User") OR IsUserInRole("Regional Admin")>
						<cfif organization.getOrganization_ID() gt 0 AND organization.getArchived() eq 0>
							<a class="btn btn-success" href="?action=archiveOrganization&view=#returnView#&Organization_ID=#organization.getOrganization_ID()#&#passbackQueryString#">Archive</a>
						<cfelseif organization.getOrganization_ID() gt 0 AND organization.getArchived() eq 1>
							<a class="btn btn-success" href="?action=unarchiveOrganization&view=#returnView#&Organization_ID=#organization.getOrganization_ID()#&#passbackQueryString#">Unarchive</a>
						</cfif>
					</cfif>
					<button type="submit" name="saveButton" class="btn btn-success">Save</button>
				</div>
			</div>

		</cfform>
	
	</cfoutput>
	
<cfelse>

	<div class="alert alert-info notFound">Organization not found</div>

</cfif>